import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAuthStore = defineStore('auth', () => {
  const isLoading = ref(false)
  const error = ref(null)

  const login = async (credentials) => {
    isLoading.value = true
    error.value = null
    
    try {
      // 模拟 API 调用
      await new Promise(resolve => setTimeout(resolve, 1000))
      
      // 模拟验证逻辑
      if (credentials.username === 'admin' && credentials.password === '123456') {
        return { success: true, message: '登录成功' }
      } else {
        throw new Error('用户名或密码错误')
      }
    } catch (err) {
      error.value = err.message
      return { success: false, message: err.message }
    } finally {
      isLoading.value = false
    }
  }

  return {
    isLoading,
    error,
    login
  }
})
